/**
 * 
 */
package sailing.filters.ui;

import java.awt.Component;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

import org.jdesktop.application.SingleFrameApplication;

import sailing.action.FilterMenuItemAction;
import sailing.filters.Filter;
import sailing.filters.FilterFactory;
import sailing.filters.FilterListModel;
import sailing.listener.UI;
import sailing.ui.AbstractJPanel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class FilterPanel extends AbstractJPanel {

	private JTable filterTable;

	private static Insets btnMargins = new Insets(2, 3, 2, 3);

	private JButton addFilterBtn, removeAllBtn, applyBtn, clearBtn;

	private JPopupMenu menu;

	private FilterListModel filterListModel;

	private FilterListPanel filterListP;

	public FilterPanel(SingleFrameApplication application) {
		super(application);
		this.initialMenu();
	}

	private void initialMenu() {
		menu = new JPopupMenu();
		List<Class<? extends Filter>> list = FilterFactory.getInstance().getFilterClassList();

		for (Class<? extends Filter> filter : list) {
			menu.add(new FilterMenuItemAction(filter, this.getFilterListModel()));
		}
	}

	private FilterListModel getFilterListModel() {
		if (filterListModel == null) {
			filterListModel = new FilterListModel();
		}
		return filterListModel;
	}

	@Override
	protected void initialPanel() {
		FormLayout layout = new FormLayout("1dlu,f:200dlu:g,5dlu,f:80dlu,1dlu", "1dlu,f:60dlu:g,1dlu");
		this.setLayout(layout);

		CellConstraints cc = new CellConstraints();

		this.add(this.getFilterTable(), cc.xy(2, 2));
		this.add(this.getBtnAreaPanel(), cc.xy(4, 2));
	}

	private Component getBtnAreaPanel() {
		JPanel side = new JPanel();
		FormLayout layout = new FormLayout("1dlu,f:70dlu,1dlu", "1dlu,f:16dlu,5dlu,f:16dlu,5dlu,f:16dlu,1dlu:g");
		side.setLayout(layout);
		CellConstraints cc = new CellConstraints();
		addFilterBtn = new JButton("添加筛选条件");
		addFilterBtn.setFont(UI.font12);
		addFilterBtn.setMargin(btnMargins);
		addFilterBtn.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				menu.show(e.getComponent(), e.getX(), e.getY());
			}

		});
		side.add(addFilterBtn, cc.xy(2, 2));

		removeAllBtn = new JButton("删除所有条件");
		removeAllBtn.setFont(UI.font12);
		removeAllBtn.setMargin(btnMargins);
		side.add(removeAllBtn, cc.xy(2, 4));

		applyBtn = new JButton("筛选");
		applyBtn.setFont(UI.font12);
		applyBtn.setMargin(btnMargins);
		side.add(applyBtn, cc.xy(2, 6));

		return side;
	}

	private Component getFilterTable() {

		if (this.filterListP == null) {
			this.filterListP = new FilterListPanel();
			this.filterListP.setModel(this.getFilterListModel());
		}

		return this.filterListP;
	}

}
